package essential.ch3;

/**
 * @Date 2021/10/23
 * @Created by Jonathan
 * 父类对象与子类对象之间进行相等比较
 */
public class PencilBox2 {
    private Box box;
    private String brand;

    public PencilBox2(int batch, String brand) {
        box = new Box(batch);
        this.brand = brand;
    }

    /**
     * 使用组合代替继承解决传递性问题
     *
     * @param o 待比较的对象
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (o instanceof PencilBox2) {
            PencilBox2 p = (PencilBox2) o;
            return box.equals(p.box) && brand == p.brand;
        }
        return false;
    }
}
